// Let's Rock!
//去除enter事件
$(document).on("focus", ".yaopinmc", function() {
	// enterToTab = false;
});
/**正则判断字符数*/
function getByteLen(val) {
	var len = 0;
	for ( var i = 0; i < val.length; i++) {
		var a = val.charAt(i);
		if (a.match(/[^\x00-\xff]/ig) != null) {
			len += 2;
		} else {
			len += 1;
		}
	}
	return len;
}

//精确查找
$(document).on('click','#jingqueczsp',function(){
	var mingcchen = $(this).parent().parent().find(".yaopinmc").val();
	$(this).parent().parent().find(".yaopinmc").val('').addClass('qiaojie');
	$(this).parent().parent().find(".sy-td-warning").css('display','none');
	$(this).parent().parent().find(".sy-td-danger").css('display','none');
	$(this).parent().parent().parent().find(".spanBaozhuanggg").text('包装规格').css('color','#777');
	$(this).parent().parent().parent().find(".cgmdc-input51").val('').attr('readonly',false);
	$(this).parent().parent().parent().find(".spanKaiyaodw").text('');
	$(this).parent().parent().parent().find(".cgmdc-input61").val('');
	$(this).parent().parent().parent().find(".cgmdc-input62").val('').attr('readonly',false);
	$(this).parent().parent().parent().find(".jine").text('');
	$(this).parent('ul').html('').css('display','none');
	$("#shangpinjqcz").modal();
	$('#shengchancj').val('');
	$('#gongyings').val('');
	$('#shangpinmc').val(mingcchen);
	showJingq();
});
//数量单位可输入
$(document).on("blur","[name='kaiyaodw']",function() {
	var kaiyaodw = $(this).val();
	if (getByteLen(kaiyaodw) > 4) {
		notifyWarning('包装规格请输入1-2个字符');
		this.focus();
		$(this).addClass("n-invalid");
		return false;
	} else {
		$(this).removeClass("n-invalid");
	}
});
var huanzheid = '';
// li标签传值
$(document).on("mouseover", ".dropdown-menu li", function() {
	$(this).addClass("hello");
});
$(document).on("mouseout", ".dropdown-menu li", function() {
	$(this).removeClass("hello");
});
//input模拟select模拟
$('.yaopinmc:first').focus();
$(document).on('focusin', ".dropdown-toggle", function() {
	var hello = $(this).siblings(".dropdown-menu").html();
	$(this).siblings(".dropdown-menu").css("display", "block");
	if (hello != "") {
	} else {
		$(this).siblings(".dropdown-menu").css('border', '0px solid #fff');
	}
});
//btn模拟select模拟
$(document).on("focusout", ".dropdown-toggle", function() {
	if ($(".dropdown-menu li").hasClass("hello")) {
		$(this).siblings(".dropdown-menu").css("display", "block");
	} else {
		$(this).siblings(".dropdown-menu").css("display", "none");
	}
});

//商品单位点击事件
$(document).on('blur','[name="kaiyaodw"]',function() {
    var kaiyaodw = $(this).val();
	var iszs = $(this).parent().parent().find('[name="iszs"]').val();
	var leixing = $(this).parent().parent().find('[name="leixing"]').val();
	var shuliang = $(this).parent().find('[name="kaiyaoliang"]').val();
	var danwei = $(this).parent().find('[name="kaiyaodw"]').val();
	var uuid = $(this).parent().parent().find('[name="ptuuid"]').val();
	var danjia = $(this).parent().parent().find('[name="danjia"]').val();
	var baozhuangdw = $(this).parent().parent().find('[name="baozhuangdw"]').val();
	var chailingsl = $(this).parent().parent().find('[name="chailingsl"]').val();
	var kucunsl = $(this).parent().parent().find('[name="kucunsl"]').val();
	var id = $(this).parent().parent().find('[name="zsShangpinid"]').val();
	var pingtaiypid = $(this).parent().parent().find('[name="ptShangpinid"]').val();
	var strli = '<li style="display:none"></li>';
	// -->标签页面赋值
	$(this).parent().parent().find('.spanKaiyaodw').text(kaiyaodw);
	$(this).parent().parent().find('[name="danwei"]').val(kaiyaodw);
	$(this).parent().find('[name="kaiyaodw"]').val(kaiyaodw);
	// <--标签页面赋值
	$(this).parents('tr').find('.yaopinmcul').append(strli);
	// 药品
	if (!id && leixing == '0') {
		var ptyaopin = getYaopinById(uuid,1);
		var yaopin = compareYaopinBykydw(ptyaopin.mingcheng,ptyaopin.baozhuanggg,kaiyaodw,ptyaopin);
		if(!!yaopin) {
			fillShangpin($(this).parents('tr').find('.yaopinmcul li'), yaopin.id, 0, leixing, uuid, yaopin.leixing, yaopin,kaiyaodw);
			id = yaopin.id;
			iszs = 0; // 诊所药品
			leixing = $(this).parent().parent().find('[name="leixing"]').val();
			shuliang = $(this).parent().find('[name="kaiyaoliang"]').val();
			danwei = $(this).parent().find('[name="kaiyaodw"]').val();
		}
		
	}
	// 耗材
	if (!id && leixing == '1') {
		var ptHaocai = getHaocaixq(uuid,1);
		var haocai = compareZsHaoCaiBykydw(ptHaocai.mingcheng,ptHaocai.baozhuanggg,kaiyaodw,ptHaocai);
		if(!!haocai) {
			fillShangpin($(this).parents('tr').find('.yaopinmcul li'), haocai.id, 0, leixing, uuid, '' , haocai,kaiyaodw);
			id = haocai.id;
			iszs = $(this).parent().parent().find('[name="iszs"]').val();
			leixing = $(this).parent().parent().find('[name="leixing"]').val();
			shuliang = $(this).parent().find('[name="kaiyaoliang"]').val();
			danwei = $(this).parent().find('[name="kaiyaodw"]').val();
		}
	}
	jisuandgspje(id, iszs, leixing, shuliang, kaiyaodw, $(this).parent().find('[name="kaiyaodw"]'));
	var kydwitem = $(this).parent().find('[name="kaiyaodw"]');
	warn(id, iszs, leixing, shuliang, kaiyaodw, $(this).parent().find('[name="kaiyaodw"]'));
	$('#formSave').trigger('click');
	$('#formSave').trigger('keyup');
});

//商品单位点击事件
$(document).on('click','.kaiyaodw li',function() {
    var kaiyaodw = $(this).text();
	var iszs = $(this).parent().parent().parent().find('[name="iszs"]').val();
	var leixing = $(this).parent().parent().parent().find('[name="leixing"]').val();
	var shuliang = $(this).parent().parent().find('[name="kaiyaoliang"]').val();
	var danwei = $(this).parent().parent().find('[name="kaiyaodw"]').val();
	var uuid = $(this).parent().parent().parent().find('[name="ptuuid"]').val();
	var danjia = $(this).parent().parent().parent().find('[name="danjia"]').val();
	var baozhuangdw = $(this).parent().parent().parent().find('[name="baozhuangdw"]').val();
	var chailingsl = $(this).parent().parent().parent().find('[name="chailingsl"]').val();
	var kucunsl = $(this).parent().parent().parent().find('[name="kucunsl"]').val();
	var id = $(this).parent().parent().parent().find('[name="zsShangpinid"]').val();
	var pingtaiypid = $(this).parent().parent().parent().find('[name="ptShangpinid"]').val();
	var kaiyaodwul = $(this).parent();
	var kaiyaodwli = kaiyaodwul.find('li');
	var strli = '<li style="display:none"></li>';
	// -->标签页面赋值
	$(this).parent().siblings(".dropdown-toggle").val(kaiyaodw).removeClass("n-invalid");
	$(this).parent(".dropdown-menu").css("display", "none");
	$(this).parent().parent().parent().find('.spanKaiyaodw').text(kaiyaodw);
	$(this).parent().parent().parent().find('[name="danwei"]').val(kaiyaodw);
	$(this).parent().parent().find('[name="kaiyaodw"]').val(kaiyaodw);
	// <--标签页面赋值
	$(this).parents('tr').find('.yaopinmcul').append(strli);
	// 药品
	if (!id && leixing == '0') {
		var ptyaopin = getYaopinById(uuid,1);
		var yaopin = compareYaopinBykydw(ptyaopin.mingcheng,ptyaopin.baozhuanggg,kaiyaodw,ptyaopin);
			if(!!yaopin) {
				fillShangpin($(this).parents('tr').find('.yaopinmcul li'), yaopin.id, 0, leixing, uuid, yaopin.leixing, yaopin,kaiyaodw);
				id = yaopin.id;
				iszs = 0; // 诊所药品
				leixing = $(this).parent().parent().parent().find('[name="leixing"]').val();
				shuliang = $(this).parent().parent().find('[name="kaiyaoliang"]').val();
				danwei = $(this).parent().parent().find('[name="kaiyaodw"]').val();
			}
	}
	// 耗材
	if (!id && leixing == '1') {
		var ptHaocai = getHaocaixq(uuid,1);
		var haocai = compareZsHaoCaiBykydw(ptHaocai.mingcheng,ptHaocai.baozhuanggg,kaiyaodw,ptHaocai);
		if(!!haocai) {
			fillShangpin($(this).parents('tr').find('.yaopinmcul li'), haocai.id, 0, leixing, uuid, '' , haocai,kaiyaodw);
			id = haocai.id;
			iszs = $(this).parent().parent().parent().find('[name="iszs"]').val();
			leixing = $(this).parent().parent().parent().find('[name="leixing"]').val();
			shuliang = $(this).parent().parent().find('[name="kaiyaoliang"]').val();
			danwei = $(this).parent().parent().find('[name="kaiyaodw"]').val();
		}
	}
	jisuandgspje(id, iszs, leixing, shuliang, kaiyaodw, $(this).parent().parent().find('[name="kaiyaodw"]'));
	var kydwitem = $(this).parent().parent().find('[name="kaiyaodw"]');
	warn(id, iszs, leixing, shuliang, kaiyaodw, $(this).parent().parent().find('[name="kaiyaodw"]'));
	$('#formSave').trigger('click');
	$('#formSave').trigger('keyup');
});

//售药患者信息折叠
$(".zhedie").hide();
$(".huanz-message-down").show();
$(".huanz-message-up").click(function() {
	$(".zhedie").hide();
	$(".huanz-message-down").show();
});
$(".huanz-message-down").click(function() {
	$(".zhedie").show();
	$(this).hide();
});
// 控制性别点击样式
$(document).on("click", ".radio-label", function() {
	$(this).addClass("on").siblings().removeClass("on");
});
$(document).on("click", ".cgmz-td-label", function() {
	$(this).addClass("on").siblings().removeClass("on");
});
$("#f-le-nianlingdw").click(function() {
	$("#leixing").css("display", "block");
});
$(document).on(
		"click",
		"#leixing li",
		function() {
			var bText = $(this).text();
			$(this).parent().siblings("#f-le-nianlingdw").text(bText)
					.removeClass("n-invalid");
			$(this).parent(".dropdown-menu").css("display", "none");
		});
//点击患者li
var huanzheid = null;
$(document).on("click", ".xingming li", function() {
	var bText = $(this).text();
	var bValue = $(this).attr("index");
	var str = bValue.split(",");
	huanzheid = str[0];
	$('#hihuanzheid').val(str[0]);
	$('#inputname').val(str[3]);
	$('#inputname').attr('readOnly', 'true');
	if (str[2] != 'null') {
		$('#inputage').val(str[2]);
	}

	$('#inputphone').val(str[4]);
	/*if(str[1] != 'null'){
	}*/
	$('#g-allergy').val(str[1]);
	$('#div-allergy').html(str[1]);

	if (str[5] == 1) {
		$('#nan').attr('checked', 'checked');
		$('#work-label').attr('class', 'radio-label cgbj-input on');
		$('#stop-label').attr('class', 'radio-label');
	} else if (str[5] == 2) {
		$('#nv').attr('checked', 'checked');
		$('#stop-label').attr('class', 'radio-label on');
		$('#work-label').attr('class', 'radio-label cgbj-input');
	}

	if (str[6] == '岁') {
		$("#f-le-nianlingdw").text('岁');
	} else {
		$("#f-le-nianlingdw").text('月');
	}
	$(this).parent().html("");
	$('#isNew').val(0);
	$('#huanzheid').val(str[0]);
	$(".f-input-li").css("display", "none");
	$('#hz_serch').val("");
	$("#mymodal-lsjz").modal('hide');
	$(".g-l-1right").css('display', 'block');
	$('.g-1lm-icon ').css("display", "block");
});

//删除患者
$('#deleteHuanzhe').click(function() {
	$('#hihuanzheid').val('');
	huanzheid = null;
	jiuzhenjlid2 = null;
	$('#inputname').val('');
	$('#inputname').removeAttr('readonly');
	$('#inputname').removeAttr('disabled');
	$('#inputage').val('');

	$('#inputphone').val('');
	$("#inputphone").removeClass("n-invalid");
	$('#g-allergy').val('');
	$("#div-allergy").text('');

	$('#nan').removeAttr('checked');
	$('#nv').removeAttr('checked');
	$('#work-label').attr('class', 'radio-label cgbj-input');
	$('#stop-label').attr('class', 'radio-label');

	$("#f-le-nianlingdw").text('岁');

	$('#isNew').val(1);
	$('#huanzheid').val('');
	$(".xiangxiandls").css('display', 'none');
	$('.weizl-fd1-1').css('margin-left', "0");
	$('.g-1lm-icon ').css("display", "none");
});
$(document).on("click", ".cgmdc-input7 ", function() {
	$(this).removeClass("n-invalid");
});
//popover
$(document).on("mouseover", ".g-mz-delete", function() {
	$(this).popover('show');
});
$(document).on("mouseout", ".g-mz-delete", function() {
	$(this).popover('hide');
});
$(document).on("mouseover", ".sy-td-warning", function() {
	$(this).popover('show');
});
$(document).on("mouseout", ".sy-td-warning", function() {
	$(this).popover('hide');
});
$(".g-1lm-icon").hover(function() {
	$(this).popover('show');
}, function() {
	$(this).popover('hide');
});
//移除售药行事件
$(document).on('click','.pointer2',function() {
			var $this = $(this);
			var $parent = $this.parent();
			var $tr = $parent.parent().parent();
			var western = $tr.parent().parent().parent();
			var length = western.find('tbody tr').length;
			
			var $parents = $this.parents('tbody');
			var wmName =$this.parents("tr").find(".sy-medicine-name")
		    var isLast = $parents.find(".sy-medicine-name").last().is(wmName);
			if (length <= 1||isLast) {
				$this.parent().parent().find("input:text").removeAttr('readOnly');
				$this.parent().parent().find("input:text").val("").removeClass("n-invalid");
				$this.parent().parent().find(".sy-td-warning").css("display","none");
				$this.parent().parent().find(".sy-td-danger").css("display","none");
				$this.parent().parent().find(".jine").text('');
				$this.parent().parent().find(".spanKaiyaodw").text('');
				$this.parent().parent().find(".spanBaozhuanggg").text('包装规格').css('color','#777');
				$("#sphejije").text("0.00元");
			} else {
				$(this).popover('hide');
				$(this).parent().parent().remove();
			}
			var jine = 0.00;
			$('#formSave').find('.jine').each(function() {
				if ($(this).text() != "") {
					jine = parseFloat(jine) + parseFloat($(this).text());
				}
			});
			jine = roundUtil(jine, 2);
			if ($('#sphejije').text() != jine + '元') {
				$('#sphejije').text(jine + '元');
			}
			$(this).parent().siblings(".hzn-name-td2").find(".dropdown-menu")
					.text('');
		});
//详细记录点击事件
$('.jzjl-xqjl-nav').click(function() {
	$('.sd-right').toggle();
});

//全局点击取消弹出
$(document).on("click", "*", function() {
	$(".r-side").css('display', 'none');
	$('#yibao-ybinfo').css('display','none');
});
$(".cYiBaoInfoNav").click(function(){
	return false;
});
//今日门诊点击事件
$('.wzl-jrmz-nav').click(function(){
	if($(this).hasClass("wzl-jrmz-navadd")){ 
		return false;
	}else{
		$('#yibao-ybinfo').hide();
		$('.r-side').toggle();
	}
});
//智能诊断点击事件
$('.cYiBaoInfoNav').click(function(){
	$(".r-side").hide();
	$(".routine-diagnosis").hide();
	$('#yibao-ybinfo').toggle();
});
//医保信息点击内容框不消失
$(document).on("mouseover","#yibao-ybinfo",function(){
	$(this).addClass("hello");
});
$(document).on("mouseout","#yibao-ybinfo",function(){
	$(this).removeClass("hello");
});
$(document).on("click","#yibao-ybinfo",function(){
	if($("#yibao-ybinfo").hasClass("hello")){
		$(this).css("display","block");
	}else{
		$(this).css("display","none");
	}
	return false;
});
var shoufeiqx;
function getCanshu(preshoufeiqx) {
	shoufeiqx = preshoufeiqx;
}
//售药添加一行
$(document).off("focusout", '.sy-medicine tbody tr .sy-medicine-name');
$(document).on('focusout','.sy-medicine tbody tr .sy-medicine-name',function() {
	if(yanhouzhsj){
		var $parents = $(this).parent().parent().parent('tbody');
		var $parent = $parents.children("tr");
		var len = $parent.length;
		var isLast = $parents.find(".sy-medicine-name").last().is($(this));
		var clone = '<tr>'
				+ '<td>'
				+ '<input type="text" class="sy-medicine-name dropdown-toggle yaopinmc" autocomplete="off" data-rule="商品名称:length(1~100)" name="shangpinmc" data-target="#msg_holder">'
				+ '<div class="g-wmfinp-warning sy-td-warning"  data-container="body" data-toggle="popover" data-placement="bottom"  data-content=""><i class="fa fa-exclamation-triangle" aria-hidden="true"></i></div>'
				+ '<div class="g-wmfinp-danger sy-td-danger"  data-container="body" data-toggle="popover" data-placement="bottom"  data-content=""><i class="fa fa-exclamation-triangle" aria-hidden="true"></i></div>'
				+ '<ul class="dropdown-menu li-width f-input-li yaopinmcul"   style="display:none" role="menu"></ul>'
				+ '</td>'
				+ '<td class="w-jiequ">'
				+ '<span class="spanBaozhuanggg guige-color">包装规格</span>'
				+ '</td>'
				+ '<td class="wm-dosage-father left">'
				+ '<input type="text" class="wm-dosage cgmdc-input51 inp-select" name="danjia" autocomplete="off" data-rule="单价:range[0~];length(~8)" data-target="#msg_holder">元 / '
				+ '<span class="spanKaiyaodw"></span>'
				+ '</td>'
				+ '<td class="wm-dosage-father wm-dosage-father2">'
				+ '<input type="text" class="wm-number cgmdc-input61" name="kaiyaoliang" placeholder="数量" autocomplete="off" data-rule="数量:integer[+];length(~8)" data-target="#msg_holder" > '
				+ '<input class="wm-dosage dropdown-toggle cgmdc-input62" placeholder="单位" data-rule="数量单位:length(1~10)" autocomplete="off" data-target="#msg_holder" name="kaiyaodw">'
				+ '<ul class="dropdown-menu li-width f-input-li kaiyaodw"  style="display:none" role="menu">'
				+ '<li>盒</li>'
				+ '<li>瓶</li>'
				+ '<li>支</li>'
				+ '<li>袋</li>'
				+ '<li>丸</li>'
				+ '<li>片</li>'
				+ '<li>粒</li>'
				+ '<li>板</li>'
				+ '<li>克</li>'
				+ '<li>个</li>'
				+ '</ul>'
				+ '</td>'
				+ '<td  style="text-align: right;padding-right:30px;"><span class="jine"></span>元</td>'
				+ '<td>'
				+ '<div class="g-mz-delete pointer2 shanchu" data-container="body" data-toggle="popover" data-placement="bottom"  data-content="清除本行"></div>'
				+ '</td>'
				+ '<input type="hidden" name="zsShangpinid">'
				+ '<input type="hidden" name="ptShangpinid">'
				+ '<input type="hidden" name="iszs">'
				+ '<input type="hidden" name="chailingsl">'
	       		+ '<input type="hidden" name="baozhuangdw">'
	       		+ '<input type="hidden" name="kucunsl">'
				+ '<input type="hidden" name="leixing">'
				+ '<input type="hidden" name="yaopinlx">'
				+ '<input type="hidden" name="ptuuid">' + '</tr>';
		if(isLast && $(this).val() != "") {
			$(this).parent().parent('tr').after(clone);
		}
	}
	yanhouzhsj = false;

});

//药品名称输入出下拉事件
$(document).on('input', '.yaopinmc', function() {
	if($(this).val().length==13){
		isTxm = true;	
	}else{
		isTxm = false;
	}
	 getZdYaopinAndHaocai(this);
	if ($(this).val() == "") {
		$(this).siblings('ul').text('');
	}
});
$(document).on('keyup', '.yaopinmc', function() {
	if ($(this).val() == "") {
		$(this).next('.yaopinmcul').html('');
		$(this).parent().parent('tr').find(".jine").text('');
		$(this).parent().parent('tr').find('[name="kaiyaodw"]').attr('readonly',false);
		var str = '<li>盒</li>'
		+ '<li>瓶</li>'
		+ '<li>支</li>'
		+ '<li>袋</li>'
		+ '<li>丸</li>'
		+ '<li>片</li>'
		+ '<li>粒</li>'
		+ '<li>板</li>'
		+ '<li>克</li>'
		+ '<li>个</li>';
		$(this).parent().parent('tr').find('.kaiyaodw').html(str);
		$(this).trigger("click");
	}
});
function fillShangpin($this, id, iszs, leixing, uuid, yaopinlx, obj,blurEvent) {
	var flag;
	$this.parent().parent().parent().addClass('notComprareThis');// 给当前行加上一个标记
	$this.parent().parent().parent().parent().find('[name="zsShangpinid"]').each(function() {
		if (!$(this).parent().hasClass('notComprareThis')) {// 当前行不比较
				if (iszs == 0 && id == $(this).val()) {
					notifyWarning('请不要添加重复商品');
					flag = true;
					return false;
				}
			}
		});
	$this.parent().parent().parent().parent().find('[name="ptShangpinid"]').each(function() {
		if (!$(this).parent().hasClass('notComprareThis')) {
			if (iszs == 1 && id == $(this).val()) {
				notifyWarning('请不要添加重复商品');
				flag = true;
				return false;
			}
		}
	});
	$this.parent().parent().parent().removeClass('notComprareThis');
	if (flag) {
		$(".f-input-li").css("display", "none");
		$this.parent().parent().parent().find('input:text,input:hidden').val('').removeClass("n-invalid");
		$this.parent().parent().parent().find(".sy-td-warning").css("display", "none");
		$this.parent().parent().find(".g-wmfinp-danger").css("display", "none");
		$this.parent().parent().parent().find("span").text('');
		$this.parent().parent().find(".jine").text('');
		$("#sphejije").text("");
		$this.parent().html('');
		return false;
	}
	if (iszs == 0) { //诊所药品
		$this.parent().parent().parent().find('[name="zsShangpinid"]').val(id);
		$this.parent().parent().parent().find('[name="ptShangpinid"]').val('');
	} else {
		$this.parent().parent().parent().find('[name="zsShangpinid"]').val('');
		$this.parent().parent().parent().find('[name="ptShangpinid"]').val(id);
		$this.parent().parent().parent().find('[name="ptuuid"]').val(uuid);
	}
	if (obj.bieming != 'null' && obj.bieming != ''&& obj.bieming != null) {
		$this.parent().parent().find('.yaopinmc').val(obj.bieming);
	} else {
		$this.parent().parent().find('.yaopinmc').val(obj.mingcheng);
	}
	$this.parent().parent().parent().find('[name="chailingsl"]').val(obj.chailingsl);
	$this.parent().parent().parent().find('[name="gongyingshang"]').val(obj.gongyingshang);
	$this.parent().parent().parent().find('[name="shengchancj"]').val(obj.shengchancj);
	$this.parent().parent().parent().find('[name="baozhuanggg"]').val(obj.baozhuanggg);
	$this.parent().parent().parent().find('[name="baozhuangdw"]').val(obj.baozhuangdw);
	$this.parent().parent().parent().find('[name="kucunsl"]').val(obj.kucunsl);
	$this.parent().parent().parent().find('[name="iszs"]').val(iszs);
	$this.parent().parent().parent().find('[name="leixing"]').val(leixing);
	$this.parent().parent().parent().find('[name="yaopinlx"]').val(yaopinlx);
	if (obj.morenkyl != null && obj.morenkyl != '') {
		$this.parent().parent().parent().find('[name="kaiyaoliang"]').val(obj.morenkyl);
	}
	// leixing = 0 (药品) = 1(耗材) ！important ==>此时的obj.leixing === yaopinlx 与判断 leixing无关
	// yaopinlx = 0 (耗材) =1 (草药 ) > 1 (成药)
	if (obj.morenkydw != null && obj.morenkydw != ''&& obj.morenkydw != '单位') { //  有默认开药单位表示为诊所*药品*
		$this.parent().parent().parent().find('[name="kaiyaodw"]').val(obj.morenkydw);
		$this.parent().parent().parent().find('.spanKaiyaodw').html(obj.morenkydw);
	} else if (iszs == 1 && yaopinlx == 1) { //iszs=1 平台药品 yaopinlx 为草药
		$this.parent().parent().parent().find('[name="kaiyaodw"]').val(obj.baozhuangdw).attr("readonly",false);
		var str = '<li>盒</li>'
			+ '<li>瓶</li>'
			+ '<li>支</li>'
			+ '<li>袋</li>'
			+ '<li>丸</li>'
			+ '<li>片</li>'
			+ '<li>粒</li>'
			+ '<li>板</li>'
			+ '<li>克</li>'
			+ '<li>个</li>';
		$this.parent().parent().parent().find('.kaiyaodw').html(str);
		var danwei = $this.parent().parent().parent().find('[name="kaiyaodw"]').val();
		if (danwei != '') {
			$this.parent().parent().parent().find('.spanKaiyaodw').html(danwei);
		}else{
			$this.parent().parent().parent().find('.spanKaiyaodw').html(obj.baozhuangdw);
		}
	} else if(iszs == 1){ //iszs=1 平台药品 yaopinlx为耗材和成药
		$this.parent().parent().parent().find('.spanKaiyaodw').html(obj.baozhuangdw);
		$this.parent().parent().parent().find('[name="kaiyaodw"]').val(obj.baozhuangdw).attr("readonly",false);
		var str = '<li>盒</li>'
			+ '<li>瓶</li>'
			+ '<li>支</li>'
			+ '<li>袋</li>'
			+ '<li>丸</li>'
			+ '<li>片</li>'
			+ '<li>粒</li>'
			+ '<li>板</li>'
			+ '<li>克</li>'
			+ '<li>个</li>';
		$this.parent().parent().parent().find('.kaiyaodw').html(str);
	}else{ // 为诊所商品且无默认单位（只能是耗材）
		// 如果有拆零单位，带拆零单位，如果没有=>包装单位
		if(obj.chailingbz == 1){ // 
			$this.parent().parent().parent().find('[name="kaiyaodw"]').val(obj.chailingdw);
			$this.parent().parent().parent().find('.spanKaiyaodw').html(obj.chailingdw);
			var strli = '<li>' + obj.baozhuangdw + '</li>' + '<li>'
						+ obj.chailingdw + '</li>';
			$this.parent().parent().parent().find('.kaiyaodw').html("");
			$this.parent().parent().parent().find('.kaiyaodw').html(strli);
		}else{
			$this.parent().parent().parent().find('[name="kaiyaodw"]').val(obj.baozhuangdw);
			$this.parent().parent().parent().find('.spanKaiyaodw').html(obj.baozhuangdw);
			$this.parent().parent().parent().find('[name="kaiyaodw"]').attr('readOnly', 'true');
			$this.parent().parent().parent().find('.kaiyaodw').html('');
		}
	}
	if (obj.xiaoshoujia != null ) {
		$this.parent().parent().parent().find('[name="danjia"]').val(roundUtil(obj.xiaoshoujia, 8));
		$this.parent().parent().parent().find('[name="danjia"]').attr('readOnly', 'true');
	}
	if (obj.baozhuanggg != null && obj.baozhuanggg != "") {
		$this.parent().parent().parent().find('.spanBaozhuanggg').text(obj.baozhuanggg).removeClass("guige-color");
	}
	var chailingbz = obj.chailingbz;
	if(obj.leixing > 0){ // 是药品进入
		if (chailingbz == 0 && iszs == 0) { // 不支持拆零spanKaiyaodw 诊所药品
			var strli = '';
			if (obj.baozhuangdw != '' && obj.baozhuangdw != null&& obj.baozhuangdw != 'null') {
				var danwei = $this.parent().parent().parent().find('[name="kaiyaodw"]').val();
				$this.parent().parent().parent().find('[name="kaiyaodw"]').val(obj.baozhuangdw).attr("readonly", "readonly");
				$this.parent().parent().parent().find('.spanKaiyaodw').html(danwei);
			} else if (yaopinlx != 1) { // 此为耗材
				strli += '<li>盒</li>' + '<li>瓶</li>' + '<li>支</li>'
						+ '<li>袋</li>' + '<li>丸</li>' + '<li>片</li>';
			}
			$this.parent().parent().parent().find('.kaiyaodw').html("");
			$this.parent().parent().parent().find('.kaiyaodw').html(strli); // 开药单位ul赋值
		} else if (chailingbz == 1 && iszs == 0) { // 支持拆零spanKaiyaodw 诊所药品
			$this.parent().parent().parent().find('[name="kaiyaodw"]').val(obj.morenkydw).attr("readonly", "readonly").css("background", "#fff");
			var danwei = $this.parent().parent().parent().find('[name="kaiyaodw"]').val();
			$this.parent().parent().parent().find('.spanKaiyaodw').text(danwei);
			var strli = '<li>' + obj.baozhuangdw + '</li>' + '<li>'
					+ obj.chailingdw + '</li>';
			$this.parent().parent().parent().find('.kaiyaodw').html("");
			$this.parent().parent().parent().find('.kaiyaodw').html(strli);
		}
		if(!!blurEvent){ // 是否为点击或者blur选择事件
			$this.parent().parent().parent().find('[name="kaiyaodw"]').val(blurEvent);
		}
	}
	var kucunsl = obj.kucunsl;
	$(".f-input-li").css("display", "none");
	warn(id, iszs, leixing, obj.morenkyl, obj.morenkydw, $this.parent().parent().parent().find('[name="kaiyaoliang"]'));
	if(!!obj.morenkydw){ // 药品计算金额
		jisuandgspje(id, iszs, leixing, obj.morenkyl, obj.morenkydw,$this.parent().parent().parent().find('[name="kaiyaoliang"]'));
	}else if(obj.leixing == 0&&iszs == 0){ // 耗材计算金额
		if(obj.chailingbz == 1){ // 耗材拆零计算金额   
			jisuandgspje(id, iszs, leixing, obj.morenkyl,obj.chailingdw,$this.parent().parent().parent().find('[name="kaiyaoliang"]'));
		}else{
			jisuandgspje(id, iszs, leixing, obj.morenkyl,obj.baozhuangdw,$this.parent().parent().parent().find('[name="kaiyaoliang"]'));
		}
		
	}
	
	$this.parent().parent().parent().parent().find("input:visible:not('.wm-group')").each(function() {
		if ($(this).val() == "") {
			$(this).focus();
			return false;
		}
	});
	yanhouzhsj = true;
	$this.parents('tr').find('.sy-medicine-name').trigger('focusout');
	$this.parent().text('');
	$('#formSave').trigger('click');
}
//点击名称li
$(document).on("click", ".yaopinmcul li.xuanzeli", function() {
			$(this).parent().parent().parent().find('[name="danjia"]')
					.removeAttr('readOnly');
			$(this).parents('tr').find('.spanBaozhuanggg').text('包装规格');
			$(this).parents('tr').find('.spanKaiyaodw').text('');
			$(this).parents('tr').find('input:text,input:hidden').val('')
					.removeClass("n-invalid");
			$(this).parent().parent().parent().find(".g-wmfinp-warning").css(
					"display", "none");
			$(this).parent().parent().parent().find(".g-wmfinp-danger").css(
					"display", "none");
			xuanzeypFlag = true;
			var id = $(this).attr('index');
			var iszs = $(this).attr('iszs');
			var leixing = $(this).attr('leixing');
			var uuid = $(this).attr('uuid');
			var yaopinlx = $(this).attr('yaopinlx');
			var obj;
			if (leixing == 0) {//药品
				if (iszs == 0) {
					obj = getYaopinById(id, iszs);
				} else {
					obj = getYaopinById(uuid, iszs);
				}
			} else {//耗材
				if (iszs == 0) {
					obj = getHaocaixq(id, iszs);
				} else {
					obj = getHaocaixq(uuid, iszs);
				}
			}
			fillShangpin($(this),id,iszs,leixing, uuid, yaopinlx, obj);
		});
//计算单个商品的总价格
function jisuandgspje(id, iszs, leixing, shuliang, danwei, obj) {
	var danjia = $(obj).parent().parent().find('[name="danjia"]').val();
	if (iszs == 0) {//诊所库存
		var shangpin;
		var zongje = '';
		if (leixing == 0) {//药品
			shangpin = getYaopinById(id, iszs);
		} else {//耗材
			shangpin = getHaocaixq(id, iszs);
		}
		var xiaoshoujia = shangpin.xiaoshoujia;//销售价
		var chailingbz = shangpin.chailingbz;
		var chailingsl = shangpin.chailingsl;
		var chailingdw = shangpin.chailingdw;
		if (chailingbz == 1 && danwei != chailingdw) {//支持拆零并且单位选择的是包装单位
			if(shuliang != '' && shuliang != null && isZhengzs(shuliang)){
				zongje = multiply(multiply(chailingsl, shuliang), xiaoshoujia);
			}
			$(obj).parent().parent().find('[name="danjia"]').val(roundUtil(multiply(chailingsl, xiaoshoujia), 8));
		} else if(xiaoshoujia){ //防止填充时出现NaN
			$(obj).parent().parent().find('[name="danjia"]').val(parseFloat(xiaoshoujia));
			if(shuliang != '' && shuliang != null){
				zongje = multiply(shuliang, xiaoshoujia);
			}
		}
		if(shuliang == '' || shuliang == null ){
			$(obj).parent().parent().find('.jine').text('');
			$(obj).parent().parent().find('[name="jine"]').attr('readOnly','readOnly');
		}else if(xiaoshoujia===0){
			$(obj).parent().parent().find('.jine').text('0.00');
		}else if(zongje){
			$(obj).parent().parent().find('.jine').text(parseFloat(roundUtil(zongje, 8)));
		}else{
			$(obj).parent().parent().find('.jine').text('0.00');
		}
	} else {
		if (shuliang != '' && shuliang != null && danjia != '') {
			zongje = roundUtil(multiply(shuliang, danjia), 8);
			if(zongje){
				$(obj).parent().parent().find('.jine').text(parseFloat(zongje));
			}else{
				$(obj).parent().parent().find('.jine').text('0.00');
			}
		} else {
			$(obj).parent().parent().find('.jine').text('0.00');
		}
	}
}

/* start-诊断相关 */
//点击诊断li
$(document).on("click",".cgmz-zhend-ul li",function(){
	var bText = $(this).text().trim().substr(0,128);
	var btext = bText.replace(/\s/g, "");
	var zhenduanstr = getZhenduanstr();
	var str = '';
	if(zhenduanstr != ''){
		str = zhenduanstr.split(':');
		str = str[0] + str[1];
	}
	if(str.indexOf(btext) >= 0 && str != '') {
	}else{
		var str = '<span index="'+$(this).attr("index")+'" iszs="'+$(this).attr("iszs")+'" class="cgmz-l2zd-dropmain">'+bText+'<span class="cgmz-l2zd-dropimg"></span></span>';
		$('.cgmz-l2zd-dropdown').append(str);
	}
	$(this).parent().parent().find('#input-diagnosis').val("");
	$(".cgmz-zhend-ul").css("display","none").text("");
	$('#hz_serch').val("");
});
//诊断鼠标离开
var zhenduanFlag = true;
// 常规门诊、微诊疗诊断blur事件
function zhenduanBlur(obj){
	var jibingmcli = $(obj).parent().find('.hello');
	var val = $(obj).val();
	if(jibingmcli.length < 1) {
		if(val!=''&&zhenduanFlag) {
			$(obj).focus();
			notifyWarning('请从诊所库存或者平台病库中选择病种！');
			return false;
		}
	}
}
//诊断内容删除事件
$(document).on('click', '.cgmz-l2zd-dropimg', function() {
    $(this).parent().remove();
});
$('#input-diagnosis').blur(function(){
	var zhenduanstr = getZhenduanstr();
	$(this).parent().find('li').each(function(){
		if($(this).hasClass('hello')) {
			zhenduanFlag = false;
		}
	});
	if(zhenduanFlag) { 
		zhenduanBlur(this);
		$(this).parent().parent().find('#input-diagnosis').val("");
		$(this).parent().find(".cgmz-zhend-ul").html("");
	}
	zhenduanFlag = true;
});
//诊断上下键选择后能再次触发下拉效果
$('#input-diagnosis').keyup(function(){
	getJibingml(this);
	$('.cgmz-zhend-ul').css('display','block');
});
//将诊断信息拼接成字符串格式，用逗号隔开
function getZhenduanstr(){
	var str = '';
	var cgmzZd = $('#cgmz-zd-dropdown').children();
	$('.cgmz-l2zd-dropdown').find('.cgmz-l2zd-dropmain').each(function(index,element){
		var s = $(this).text().split(/\s+/);
		if(index==0) {
			if(!!cgmzZd.eq(index).attr('iszs')){
				str += s[0] + ':' + s[1] + ':' + cgmzZd.eq(index).attr('iszs') + ':' + cgmzZd.eq(index).attr('index');
			}else{
				str += s[0] + ':' + s[1];
			}
		}else{
			if(!!cgmzZd.eq(index).attr('iszs')){
				str += ',' + s[0] + ':' + s[1] + ':' + cgmzZd.eq(index).attr('iszs') + ':' + cgmzZd.eq(index).attr('index');
			}else{
				str += ',' + s[0] + ':' + s[1];
			}
		}
	});
	return str;
}
/* end-诊断相关 */
//检测必须从平台或者诊所库中选择
function jianceShangpin(val, zsShangpinid, ptShangpinid, ptuuid, leixing, obj) {
	var yaopinmcli = $(obj).parent().find('.hello');
	if (yaopinmcli.length < 1) {
		if (val != '' && zsShangpinid == '' && ptShangpinid == '') {
			if (!$(obj).parent().find(".dropdown-menu li").hasClass("hello")) {
				$(obj).focus();
				ZIDONGJK == "true"?notifyWarning('请从诊所库存或者平台药库中选择商品！'):notifyWarning('请从诊所库存中选择商品！');
				xuanzeypFlag = false;
			} else {
				xuanzeypFlag = true;
			}
		}
		if (val != '' && zsShangpinid != '' && zsShangpinid != 'null') {
			var shangpin;
			if (leixing == 0) {//药品
				shangpin = getYaopinById(zsShangpinid, 0);
			} else {//耗材
				shangpin = getHaocaixq(zsShangpinid, 0);
			}
			if (shangpin.bieming != null && shangpin.bieming != '') {
				if (val != shangpin.bieming) {
					$(obj).focus();
					ZIDONGJK == "true" ?notifyWarning('请从诊所库存或者平台药库中选择药品！'):notifyWarning('请从诊所库存中选择药品！');
					xuanzeypFlag = false;
				}
			} else {
				if (val != shangpin.mingcheng) {
					$(obj).focus();
					ZIDONGJK == "true" ?notifyWarning('请从诊所库存或者平台药库中选择药品！'):notifyWarning('请从诊所库存中选择药品！');
					xuanzeypFlag = false;
				} else {
					xuanzeypFlag = true;
				}
			}

		}
		if (val != '' && ptuuid != '' && ptuuid != 'null'
				&& ptuuid != 'undefined') {
			var shangpin;
			if (leixing == 0) {//药品
				shangpin = getYaopinById(ptuuid, 1);
			} else {//耗材
				shangpin = getHaocaixq(ptuuid, 1);
			}
			if (val != shangpin.mingcheng) {
				$(obj).focus();
				ZIDONGJK == "true" ?notifyWarning('请从诊所库存或者平台药库中选择药品！'):notifyWarning('请从诊所库存中选择药品！');
				xuanzeypFlag = false;
			} else {
				xuanzeypFlag = true;
			}
		}

	}
}
$(document).on(
		'blur',
		'[name="shangpinmc"]',
		function() {
			var val = $(this).val();
			var zsShangpinid = $(this).parent().parent().find(
					'[name="zsShangpinid"]').val();
			var ptShangpinid = $(this).parent().parent().find(
					'[name="ptShangpinid"]').val();
			var ptuuid = $(this).parent().parent().find('[name="ptuuid"]')
					.val();
			var leixing = $(this).parent().parent().find('[name="leixing"]')
					.val();

			jianceShangpin(val, zsShangpinid, ptShangpinid, ptuuid, leixing,
					this);
		});

//商品数量改变
$(document).on('keyup', '[name="kaiyaoliang"]', function() {
	var id = $(this).parent().parent().find('[name="zsShangpinid"]').val();
	var iszs = $(this).parent().parent().find('[name="iszs"]').val();
	var leixing = $(this).parent().parent().find('[name="leixing"]').val();
	var shuliang = $(this).val();
	var danwei = $(this).parent().find('[name="kaiyaodw"]').val();
	jisuandgspje(id, iszs, leixing, shuliang, danwei, $(this));
	warn(id, iszs, leixing, shuliang, danwei, $(this));
	$('#formSave').trigger('click');
	$('#formSave').trigger('keyup');
});

//商品单价改变
$(document).on(
		'keyup',
		'[name="danjia"]',
		function() {
			var id = $(this).parent().parent().find('[name="zsShangpinid"]')
					.val();
			var iszs = $(this).parent().parent().find('[name="iszs"]').val();
			var leixing = $(this).parent().parent().find('[name="leixing"]')
					.val();
			var shuliang = $(this).parent().parent().find(
					'[name="kaiyaoliang"]').val();
			var danwei = $(this).parent().find('[name="kaiyaodw"]').val();
			jisuandgspje(id, iszs, leixing, shuliang, danwei, $(this));
			warn(id, iszs, leixing, shuliang, danwei, $(this));
			$('#formSave').trigger('click');
			$('#formSave').trigger('keyup');
		});

//库存不足&不在库提醒
function warn(id, iszs, leixing, shuliang, danwei, obj) {
	if (iszs == 0) {//诊所库存
		var shangpin;
		if (leixing == 0) {//药品
			shangpin = getYaopinById(id, iszs);
		} else {//耗材
			shangpin = getHaocaixq(id, iszs);
		}
		var kucunsl = shangpin.kucunsl;
		var chailingbz = shangpin.chailingbz;
		var chailingsl = shangpin.chailingsl;
		var chailingdw = shangpin.chailingdw;
		if (chailingbz == 1 && danwei != chailingdw) {//支持拆零并且单位选择的是包装单位
			shuliang = multiply(chailingsl, shuliang);
		}
		if (shuliang >= kucunsl) {//库存不足
			var danger = obj.parent().parent().find('.sy-td-danger').css(
					"display");
			if (danger == 'block') {
				obj.parent().parent().find('.sy-td-warning').css('right',
						'35px');
			} else {
				obj.parent().parent().find('.sy-td-warning').css('right',
						'12px');
			}
			obj.parent().parent().find('.sy-td-warning')
					.css('display', 'block');
			obj.parent().parent().find('.sy-td-warning').attr('data-content',
					'该商品库存不足');
		} else {
			obj.parent().parent().find('.sy-td-warning').css('display', 'none');
		}
	} else {//不在库
		var danger = obj.parent().parent().find('.sy-td-danger').css("display");
		if (danger == 'block') {
			obj.parent().parent().find('.sy-td-warning').css('right', '35px');
		} else {
			obj.parent().parent().find('.sy-td-warning').css('right', '12px');
		}
		obj.parent().parent().find('.sy-td-warning').css('display', 'block');
		obj.parent().parent().find('.sy-td-warning').attr('data-content',
				'该商品不在您的诊所库存中');
	}
}

var saveOrUpdate = 'save';// 
//验证
$('#formSave').validator({
		fields: {
			'xingming': '姓名:length(1~64)',
			'leixing': 'checked',
			'shoujihm':'mobile',
			'nianling':'integer[+]; range[~200]',
			'guominshi': 'length(1~200)',
			'shangpinmc':'商品名称:length(1~100)',
			'danjia':'单价:range[0~];length(~10)',
			'kaiyaoliang':'数量:range(0~);length(~8)',
			'jine':'金额:range[0~];length(~18)',
		}
	});
	//光标function
	function huoQuGuangBiao(yuanshu,msg,loading){
		notifyWarning(msg);
		yuanshu.focus();
		loading.css({"display":"none","z-index":"5"});
	};
	
	// 提交
	validateform();
	function validateform() {
		$('#formSave').on('valid.form', function(e){
			$('#hishouyaoParams').val(tableDataToJson('tbShouyao'));
			$('#hinianlingdw').val($('#f-le-nianlingdw').text());
			var guangBiao = true;
			$('#tbodyShouyao tr').each(function(i,e){
			 var yaopinxx1=$(this).find('.yaopinmc').val();//商品名称
			 var yaopinxx2=$(this).find('.cgmdc-input51').val();//单价
			 var yaopinxx3=$(this).find('.cgmdc-input61').val();//数量
			 var yaopinxx4=$(this).find('.cgmdc-input62').val();//数量单位
			 //var yaopinxx5=$(this).find('.cgmdc-input7').val();//金额
			 $('#hizsJiuzhenzd').val(getZhenduanstr());
			 if(i==0){
				 if(yaopinxx1==""){
						$(this).find('.yaopinmc').addClass('n-invalid');
	  					if(guangBiao)
	  					huoQuGuangBiao($(this).find('.yaopinmc'),'请完成商品名称',$("#loading"));
	  					guangBiao = false;
					};
				 if(yaopinxx2==""){
						$(this).find('.cgmdc-input51').addClass('n-invalid');
	  					if(guangBiao)
	  					huoQuGuangBiao($(this).find('.cgmdc-input51'),'请完成商品单价',$("#loading"));
	  					guangBiao = false;
					};
				 if(yaopinxx3==""){
						$(this).find('.cgmdc-input61').addClass('n-invalid');
	  					if(guangBiao)
	  					huoQuGuangBiao($(this).find('.cgmdc-input61'),'请完成商品数量',$("#loading"));
	  					guangBiao = false;
					};	
				 if(yaopinxx4==""){
						$(this).find('.cgmdc-input62').addClass('n-invalid');
	  					if(guangBiao)
	  					huoQuGuangBiao($(this).find('.cgmdc-input62'),'请完成商品数量单位',$("#loading"));
	  					guangBiao = false;
					};	
			 }else{
				 if(yaopinxx1!=""||yaopinxx2!=""||yaopinxx3!=""||yaopinxx4!=""){
					 if(yaopinxx1==""){
							$(this).find('.yaopinmc').addClass('n-invalid');
		  					if(guangBiao)
		  					huoQuGuangBiao($(this).find('.yaopinmc'),'请完成商品名称',$("#loading"));
		  					guangBiao = false;
						};
					 if(yaopinxx2==""){
							$(this).find('.cgmdc-input51').addClass('n-invalid');
		  					if(guangBiao)
		  					huoQuGuangBiao($(this).find('.cgmdc-input51'),'请完成商品单价',$("#loading"));
		  					guangBiao = false;
						};
					 if(yaopinxx3==""){
							$(this).find('.cgmdc-input61').addClass('n-invalid');
		  					if(guangBiao)
		  					huoQuGuangBiao($(this).find('.cgmdc-input61'),'请完成商品数量',$("#loading"));
		  					guangBiao = false;
						};	
					 if(yaopinxx4==""){
							$(this).find('.cgmdc-input62').addClass('n-invalid');
		  					if(guangBiao)
		  					huoQuGuangBiao($(this).find('.cgmdc-input62'),'请完成商品数量单位',$("#loading"));
		  					guangBiao = false;
						};	
				 }
			 }
			 
			});
			if($("#sphejije").text()=="0.00元"&&guangBiao){
				if($('#hizhuangtai').val()==1){
					guangBiao=true;
				}else{
					notifyWarning("合计金额为0.00元，请检查药单是否正确");
					guangBiao = false;
				}
			}
			if(saveOrUpdate == 'save'&&guangBiao&&xuanzeypFlag){
				//按钮不可重复点击
 				if($('#hizhuangtai').val()==2) {
 					//设置收费按钮不可点击
 				$('#btnShoufei').attr('disabled',true);
 				}else{
 					//设置保存按钮不可点击
					$('#btnBaocun').attr('disabled',true);
 				}
				$.ajax({
				    url:path+'/changguimz/xinjianShouyao',
				    type:'post',
				    beforeSend: function () {
	 					$("#loading").css({"display":"block","z-index":"5"})
					  },
				    complete: function () {
						$("#loading").css({"display":"none","z-index":"5"})
				    },
				    data:$('#formSave').serializeArray(),
				    success:function(data) {
						var sjc=new Date().getTime(); 
	 					if(shoufeiqx=='true'&&$('#hizhuangtai').val()==2) {//有收费的权限并且是收费,跳转到收费页面
	 						window.location.href= path+'/jump/toShoufeixq?type=shouyao&ID='+data;
	 					}else if(shoufeiqx=='false'&&$('#hizhuangtai').val()==2){//没有有收费的权限并且是收费,
	  						//收费成功提示
	  						var presuccmsg=encodeURI(encodeURI('已提交给收费人员'));
	  						window.location.href=  path+'/jump/toShouyao?msg='+presuccmsg+'&sjc='+sjc;
	  	 				  	return false;
	  					}else if($('#hizhuangtai').val()==1){
	  						//保存成功提示
	  						var presuccmsg=encodeURI(encodeURI('保存成功'));
	  						window.location.href=  path+'/jump/toShouyao?msg='+presuccmsg+'&sjc='+sjc;
	  	 				  	return false;
	  					}
					}
				});
			}else if(guangBiao&&xuanzeypFlag){
				//按钮不可重复点击
 				if($('#hizhuangtai').val()==2) {
 					//设置收费按钮不可点击
 				$('#btnShoufei').attr('disabled',true);
 				}else{
 					//设置保存按钮不可点击
					$('#btnBaocun').attr('disabled',true);
 				}
				$.ajax({
				    url:path+'/changguimz/xiugaiShouyao',
				    type:'post',
				    beforeSend: function () {
	 					$("#loading").css({"display":"block","z-index":"5"})
					  },
				    complete: function () {
						$("#loading").css({"display":"none","z-index":"5"});
				    },
				    data:$('#formSave').serializeArray(),
				    success:function(data) {
						var sjc=new Date().getTime(); 
	 					 if(shoufeiqx == 'true'&&$('#hizhuangtai').val()==2) {//有收费的权限并且是收费,跳转到收费页面
	  						window.location.href= path+'/jump/toShoufeixq?type=shouyao&ID='+data;
	  					}else if(shoufeiqx == 'false'&&$('#hizhuangtai').val()==2){//没有有收费的权限并且是收费,
	  						//收费成功提示
	  						var presuccmsg=encodeURI(encodeURI('已提交给收费人员'));
	  						window.location.href=  path+'/jump/toShouyao?msg='+presuccmsg+'&sjc='+sjc;
	  	 				  	return false;
	  					}else if($('#hizhuangtai').val()==1){
	  						//保存成功提示
	  						var presuccmsg=encodeURI(encodeURI('保存成功'));
	  						window.location.href=  path+'/jump/toShouyao?msg='+presuccmsg+'&sjc='+sjc;
	  	 				  	return false;
	  					}
					}
				});
			}	
		});
	}
	
	$('#btnShoufei').click(function(){
		A = 0;
		$('#hizhuangtai').val(2);//待收费状态
	});
	$('#btnBaocun').click(function(){
		A = 0;
		$('#hizhuangtai').val(1);//保存状态
	});
	$('#yibao-shoufei').click(function(){
		var isKehud = $('#isKehud').val();
		if(isKehud == 0) {
			notifyWarning("请检查医保设置或下载医保客户端");
		}
	});
	$('#yibao-yujies').click(function(){
		var isKehud = $('#isKehud').val();
		if(isKehud == 0) {
			notifyWarning("请检查医保设置或下载医保客户端");
			return false;
		}
		$('#yibao-yujies-out').modal('toggle');
	});
	
	//计算总金额
	$('#formSave').click(function(){
		var jine = 0.00;
		$('#formSave').find('.jine').each(function(){
			if($(this).text()!="") {
				jine = parseFloat(jine) + parseFloat($(this).text());
			}
		});
		jine=roundUtil(jine,2);
		if($('#sphejije').text()!= jine+'元'){
			$('#sphejije').text(jine+'元');
		}
	}).keyup(function(event){
		keyCode = event.which;
		var jine = 0.00;
		$('#formSave').find('.jine').each(function(){
			if($(this).text()!="") {
				jine = parseFloat(jine) + parseFloat($(this).text());
			}
		});
		jine=roundUtil(jine, 2);
		if($('#sphejije').text()!= jine+'元'){
			$('#sphejije').text(jine+'元');
		}
	});
	
/******今日售药部分*******/
// 加载今日售药列表
showJinriShouyao();
function showJinriShouyao(){
var totalNumber = 0;
$.ajax({
    type:"get",
    //获取json文件路径
    url:path+"/changguimz/getCount",
    //传送数据
    data:{zhuangtai:0,yewurk:2},
    async:false,
    //请求成功完成后要执行的方法
    success: function(data){
    	totalNumber = data;
		$('#menzhensl').text(data);
    	if(data==''){
			$(".wzl-jrmz-nav").addClass("wzl-jrmz-navadd");
		}
    }
});
$.ajax({
    type:"get",
    //获取json文件路径
    url:path+"/changguimz/getCount",
    //传送数据
    data:{zhuangtai:1,yewurk:2},//保存
    async:false,
    //请求成功完成后要执行的方法
    success: function(data){
		$('#zancunsl').text(data);
    }
});
$.ajax({
    type:"get",
    //获取json文件路径
    url:path+"/changguimz/getCount",
    //传送数据
    data:{zhuangtai:2,yewurk:2},//代收费
    async:false,
    //请求成功完成后要执行的方法
    success: function(data){
		$('#weishoufsl').text(data);
    }
});
$.ajax({
    type:"get",
    //获取json文件路径
    url:path+"/changguimz/getCount",
    //传送数据
    data:{zhuangtai:3,yewurk:2},//已收费
    async:false,
    //请求成功完成后要执行的方法
    success: function(data){
		$('#yishoufsl').text(data);
    }
});
function createJinrimz(name) {
var container = $('#pagination-'+name);
container.pagination({
	dataSource:path+'/changguimz/getByPage?yewurk=2',
	locator: 'entities',
	pageSize: 5,
	totalNumber: totalNumber,
	callback: function(data, pagination) {
		$("#tbjinrmz").html('');
		var content = '';
		$.each(data, function (i, item) {
			var shoufeizt = '';
			if(this.zhuangtai == 1) {
				var shoufeizt = '保存';
			}else if(this.zhuangtai == 2) {
				var shoufeizt = '待收费';
			}else if(this.zhuangtai == 3) {
				var shoufeizt = '已收费';
			}
			if(this.yewurk == 1) {
				var yewurk = '微诊疗';
			}else if(this.yewurk == 3){
				var yewurk = '常规门诊';
			}

			content +='<tr onclick="showOneJinriShouyao('+this.id+')">';
			content +=' <td width="20%">'+this.xiugaisj+'</td>';
			content +='<td width="60%" style="overflow:hidden;white-space: nowrap;text-overflow: ellipsis;">'+this.huanzhemc+'</td>';
			if(shoufeizt =='已收费'){
				content +='<td width="20%" class=" cgmz-r1-ysf">'+shoufeizt+'</td>';
			}else if(shoufeizt == '待收费') {
				content +='<td  width="20%" class=" cgmz-r1-wsf">'+shoufeizt+'</td>';
			}else{
				content +='<td width="20%" class="">'+shoufeizt+'</td>';
			}
			content +="</tr>";
		});
		$("#tbjinrmz").append(content);
		$(".wzl-jrmz-nav").removeClass("wzl-jrmz-navadd");
	}
});
	return container;
}
	createJinrimz('fenyjrmz');
	if(totalNumber <= 5) {
		$('#pagination-fenyjrmz').css('display','none');
	}
};

var shoufeizt;
// 点击今日售药一行
function showOneJinriShouyao(jiuzhenjlid){
	saveOrUpdate = 'update';
	$.ajax({
		url:path+'/changguimz/getById',
		type:'post',
		data:{id:jiuzhenjlid},
		success:function(data) {
			shoufeizt=data.zsJiuzhenjl.zhuangtai;
			if(data.zsJiuzhenjl.zhuangtai==1) {
				// 保存状态，允许修改
				$(".r-side").css("display","none");
				$(".g-l-1left").css("display","block");
				$(".quan-zhedang").css("display","none");
				$("#btnShoufei").show();
				$("#btnBaocun").show();
				$(".zhedie").show();
				$(".huanz-message-down").hide();
				$("#inputname").attr("disabled","disabled");
				$('[name="danjia"]').attr("readonly","readonly")
				$("#deleteHuanzhe").css("display","block");
			}else{
				// 待收费和已收费状态，不允许修改
				$(".r-side").css("display","none");
				$(".g-l-1left").css("display","block");
				$(".quan-zhedang").css("display","block");
				$("#btnShoufei").hide();
				$("#btnBaocun").hide();
				$(".zhedie").show();
				$(".huanz-message-down").hide();
			}
			$('#hijiuzhenjlid').val(data.zsJiuzhenjl.id);
			$('#sphejije').text('￥'+roundUtil(data.zsJiuzhenjl.hejije, 2));
			/****        患者部分       ****/
    		var huanzhexx = data.huanzhexx;
    		huanzheid=huanzhexx.id;
    		$('#hihuanzheid').val(huanzhexx.id);
    		$(".xiangxiandls").css('display','block');
    		//$('.weizl-fd1-1').css('margin-left',"14rem");
    		$('#huanzheid').val(huanzhexx.id);
    		$('#inputname').val(huanzhexx.xingming);
    		$('#inputage').val(huanzhexx.nianling);
    		$('#inputphone').val(huanzhexx.shoujihm);
    		$('#g-allergy').val(huanzhexx.guominshi);
    		$('#div-allergy').html(huanzhexx.guominshi);
    		if(huanzhexx.xingbie==1) {
    			$('#nan').attr('checked','checked');
    			$('#work-label').attr('class','radio-label cgbj-input on');
    			$('#stop-label').attr('class','radio-label');
    		}else if(huanzhexx.xingbie==2){
    			$('#nv').attr('checked','checked');
    			$('#stop-label').attr('class','radio-label on');
    			$('#work-label').attr('class','radio-label cgbj-input');
    		}
    		if(huanzhexx.nianlingdw=='岁') {
    			$("#f-le-nianlingdw").text('岁');
    		}else {
    			$("#f-le-nianlingdw").text('月');
    		}
			//诊断
    		var zsJiuzhenzds = data.zsJiuzhenzds;
    		var zdStr = '';
    		$(zsJiuzhenzds).each(function(){
    			var iszs = '';
    			var index = '';
    			if(!!this.zhensuojbid){
    				iszs = '1';
    				index = this.zhensuojbid+'';
    			}else{
    				iszs = '0';
    				index = this.pingtaijbid+'';
    			}
    			var icd10 = this.icd10;
    			if(!!icd10 && icd10 != 'undefined'){
    				zdStr += '<span iszs='+iszs+' index='+index+' class="cgmz-l2zd-dropmain">'+this.jibingmc+'&nbsp;&nbsp;&nbsp;'+this.icd10+'<span class="cgmz-l2zd-dropimg"></span></span>';
    			}else{
    				zdStr += '<span iszs='+iszs+' index='+index+' class="cgmz-l2zd-dropmain">'+this.jibingmc+'<span class="cgmz-l2zd-dropimg"></span></span>';
    			}
    		});
    		$('.cgmz-l2zd-dropdown').html("");
    		$('.cgmz-l2zd-dropdown').append(zdStr);
    		/***      药品耗材部分       ***/
    		var content = '';
    		var exZsChengyaocfs = data.exZsChengyaocfs;
    		if(exZsChengyaocfs!="") {
    			var yaopins = exZsChengyaocfs[0].exYaopins;
    			$(yaopins).each(function(){
    				content += '<tr>'+
    				'<td>'+
    				'<input type="text" class="sy-medicine-name dropdown-toggle yaopinmc" autocomplete="off" data-rule="商品名称:length(1~100)" name="shangpinmc" data-target="#msg_holder" value="'+this.yaopinmc+'">'+
    				'<div class="sy-td-warning"  data-container="body" data-toggle="popover" data-placement="bottom"  data-content=""><i class="fa fa-exclamation-triangle" aria-hidden="true"></i></div>'+
	        		'<div class="sy-td-danger"  data-container="body" data-toggle="popover" data-placement="bottom"  data-content=""><i class="fa fa-exclamation-triangle" aria-hidden="true"></i></div>'+
	        		'<ul class="dropdown-menu li-width f-input-li yaopinmcul"   style="display:none;width:260%" role="menu"></ul>'+
    				'</td>'+
    				'<td class="w-jiequ">';
    				if(this.baozhuanggg !=""){
    					content +='<span class="spanBaozhuanggg">'+this.baozhuanggg+'</span>';
    				}else{
    					content +='<span class="spanBaozhuanggg guige-color">包装规格</span>';
    				}
    				content +='</td>'+
    				'<td class="wm-dosage-father left">';
    				if(this.zhensuoypid!=null) {
    					content += '<input type="text" class="wm-dosage cgmdc-input51 inp-select" name="danjia" autocomplete="off" data-rule="单价:range[0~];length(~8)" readonly="readonly" data-target="#msg_holder" value="'+roundUtil(this.danjia, 8)+'">元/';
    				}else{
    					content += '<input type="text" class="wm-dosage cgmdc-input51 inp-select" name="danjia" autocomplete="off" data-rule="单价:range[0~];length(~8)" data-target="#msg_holder" value="'+roundUtil(this.danjia, 8)+'">元/';
    				}
    				 if(this.kaiyaodw == this.baozhuangdw &&this.chailingbz == '1') {
    					var danjiadw = this.baozhuangdw;
    				} else {
    					var danjiadw = this.kaiyaodw;
    				}
    				content += '<span class="spanKaiyaodw">'+danjiadw+'</span>'+
		          	'</td>'+
		          	'<td class="wm-dosage-father wm-dosage-father2 dropdown-search-li">'+
		          	'<input type="text" class="wm-number cgmdc-input61" value="'+this.kaiyaoliang+'" placeholder="数量" name="kaiyaoliang" autocomplete="off" data-rule="数量:integer[+];length(~8)" data-target="#msg_holder" > ';
		          	if(this.chailingbz == null){
		          		content +='<input class="wm-dosage dropdown-toggle cgmdc-input62" value="'+this.kaiyaodw+'" placeholder="单位" data-rule="数量单位:length(1~10)" readonly="readonly" autocomplete="off" data-target="#msg_holder" name="kaiyaodw">';
		          	}else if(this.chailingbz == 0){
		          		content +='<input class="wm-dosage dropdown-toggle cgmdc-input62" value="'+this.kaiyaodw+'" placeholder="单位" data-rule="数量单位:length(1~10)" readonly="readonly" autocomplete="off" data-target="#msg_holder" name="kaiyaodw">';
		          	}else{
		          		content +='<input class="wm-dosage dropdown-toggle cgmdc-input62" value="'+this.kaiyaodw+'" placeholder="单位" data-rule="数量单位:length(1~10)" autocomplete="off" data-target="#msg_holder" name="kaiyaodw" style="background-color:#fff;">'+
		          		'<ul class="dropdown-menu li-width f-input-li kaiyaodw"  style="display:none;top: 3.5rem;" role="menu">'+
		          		'<li>'+this.baozhuangdw+'</li>'+
		          		'<li>'+this.chailingdw+'</li>';
		          	}
             		
             		content +='</ul>'+
		          	'</td >'+
		          	'<td style="text-align: right;padding-right:30px;"><span class="jine">'+roundUtil(this.jine, 8)+'</span>元</td>'+
		          	'<td>'+
		       		'<div class="g-mz-delete pointer2 shanchu" data-container="body" data-toggle="popover" data-placement="bottom"  data-content="清除本行"></div>'+
		          	'</td>'+
		          	'<input type="hidden" name="zsShangpinid" value="'+this.zhensuoypid+'">'+
	       		   	'<input type="hidden" name="ptShangpinid" value="'+this.pingtaiypid+'">'+
	       		   	'<input type="hidden"  style="display:none;" name="ptuuid" value="'+this.ptuuid+'">'+
	       		    '<input type="hidden" name="chailingsl" value="'+this.chailingsl+'">'+
		       		'<input type="hidden" name="baozhuangdw" value="'+this.baozhuangdw+'">'+
					'<input type="hidden" name="shengchancj" value="'+this.shengchancj+'">'+
					'<input type="hidden" name="baozhuanggg" value="'+this.baozhuanggg+'">'+
					'<input type="hidden" name="gongyingshang" value="'+this.gongyingshang+'">'+
		       		'<input type="hidden" name="kucunsl" value="'+this.kucunsl+'">'+
	       		   	'<input type="hidden" name="iszs" value="'+this.iszs+'">'+
	       		   	'<input type="hidden" name="leixing" value="0">'+
	       		   	'<input type="hidden" name="yaopinlx" value="'+this.leixing+'">'+
		          	'</tr>';
    			});
    		}
    		
    		var exZsCaoyaocfs = data.exZsCaoyaocfs;
    		if(exZsCaoyaocfs!="") {
    			var yaopins = exZsCaoyaocfs[0].exYaopins;
    			$(yaopins).each(function(){
    				content += '<tr>'+
    				'<td>'+
    				'<input type="text" class="sy-medicine-name dropdown-toggle yaopinmc" autocomplete="off" data-rule="商品名称:length(1~100)" name="shangpinmc" data-target="#msg_holder" value="'+this.yaopinmc+'">'+
    				'<div class="sy-td-warning"  data-container="body" data-toggle="popover" data-placement="bottom"  data-content=""><i class="fa fa-exclamation-triangle" aria-hidden="true"></i></div>'+
	        		'<div class="sy-td-danger"  data-container="body" data-toggle="popover" data-placement="bottom"  data-content=""><i class="fa fa-exclamation-triangle" aria-hidden="true"></i></div>'+
	        		'<ul class="dropdown-menu li-width f-input-li yaopinmcul"   style="display:none;width:260%" role="menu"></ul>'+
    				'</td>'+
    				'<td class="w-jiequ">';
    				if(this.baozhuanggg !=""){
    					content +='<span class="spanBaozhuanggg">'+this.baozhuanggg+'</span>';
    				}else{
    					content +='<span class="spanBaozhuanggg guige-color">包装规格</span>';
    				}
    				content +='</td>'+
    				'<td class="wm-dosage-father left">';
    				if(this.zhensuoypid!=null) {
    					content += '<input type="text" readOnly="readOnly" class="wm-dosage cgmdc-input51 inp-select" name="danjia" autocomplete="off" data-rule="单价:range[0~];length(~8)" data-target="#msg_holder" value="'+roundUtil(this.danjia, 8)+'">元/';
    				}else{
    					content += '<input type="text" class="wm-dosage cgmdc-input51 inp-select" name="danjia" autocomplete="off" data-rule="单价:range[0~];length(~8)" data-target="#msg_holder" value="'+roundUtil(this.danjia, 8)+'">元/';
    				}
    				if(this.danwei == this.baozhuangdw &&this.chailingbz == '1') {
    					var danjiadw = this.baozhuangdw;
    				} else {
    					var danjiadw = this.morenkydw;
    				}
    				content += '<span class="spanKaiyaodw">'+danjiadw+'</span>'+
		          	'</td>'+
		          	'<td class="wm-dosage-father wm-dosage-father2 dropdown-search-li">'+
		          	'<input type="text" class="wm-number cgmdc-input61" value="'+this.danjiliang+'" placeholder="数量" name="kaiyaoliang" autocomplete="off" data-rule="数量:integer[+];length(~8)" data-target="#msg_holder" > ';
		          	if(this.chailingbz == null){
		          		content +='<input class="wm-dosage dropdown-toggle cgmdc-input62" value="'+this.danwei+'" placeholder="单位" data-rule="数量单位:length(1~10)" readonly="readonly" autocomplete="off" data-target="#msg_holder" name="kaiyaodw">';
		          	}else if(this.chailingbz == 0){
		          		content +='<input class="wm-dosage dropdown-toggle cgmdc-input62" value="'+this.danwei+'" placeholder="单位" data-rule="数量单位:length(1~10)" readonly="readonly" autocomplete="off" data-target="#msg_holder" name="kaiyaodw">';
		          	}else{
		          		content +='<input class="wm-dosage dropdown-toggle cgmdc-input62" value="'+this.danwei+'" placeholder="单位" data-rule="数量单位:length(1~10)" autocomplete="off" data-target="#msg_holder" name="kaiyaodw" style="background-color:#fff;">'+
		          		'<ul class="dropdown-menu li-width f-input-li kaiyaodw"  style="display:none;top: 3.5rem;" role="menu">'+
		          		'<li>'+this.baozhuangdw+'</li>'+
		          		'<li>'+this.chailingdw+'</li>';
		          	}
    				content +='</ul>'+
		          	'</td >'+
		          	'<td style="text-align: right;padding-right:30px;"><span class="jine">'+roundUtil(this.jine, 8)+'</span>元</td>'+
		          	'<td>'+
		       		'<div class="g-mz-delete pointer2 shanchu" data-container="body" data-toggle="popover" data-placement="bottom"  data-content="清除本行"></div>'+
		          	'</td>'+
		          	'<input type="hidden" name="zsShangpinid" value="'+this.zhensuoypid+'">'+
	       		   	'<input type="hidden" name="ptShangpinid" value="'+this.pingtaiypid+'">'+
	       		   	'<input type="hidden"  style="display:none;" name="ptuuid" value="'+this.ptuuid+'">'+
	       		   	'<input type="hidden" name="chailingsl" value="'+this.chailingsl+'">'+
		       		'<input type="hidden" name="baozhuangdw" value="'+this.baozhuangdw+'">'+
					'<input type="hidden" name="shengchancj" value="'+this.shengchancj+'">'+
					'<input type="hidden" name="baozhuanggg" value="'+this.baozhuanggg+'">'+
					'<input type="hidden" name="gongyingshang" value="'+this.gongyingshang+'">'+
		       		'<input type="hidden" name="kucunsl" value="'+this.kucunsl+'">'+
	       		   	'<input type="hidden" name="iszs" value="'+this.iszs+'">'+
	       		   	'<input type="hidden" name="leixing" value="0">'+
	       		   	'<input type="hidden" name="yaopinlx" value="'+this.leixing+'">'+
		          	'</tr>';
    			});
    		}
    		var zsJiuzhenhcs = data.exZsJiuzhenhcs;
    		if(zsJiuzhenhcs!="") {
    			$(zsJiuzhenhcs).each(function(){
    				content += '<tr>'+
    				'<td>'+
    				'<input type="text" class="sy-medicine-name dropdown-toggle yaopinmc" autocomplete="off" data-rule="商品名称:length(1~100)" name="shangpinmc" data-target="#msg_holder" value="'+this.mingcheng+'">'+
    				'<div class="sy-td-warning"  data-container="body" data-toggle="popover" data-placement="bottom"  data-content=""><i class="fa fa-exclamation-triangle" aria-hidden="true"></i></div>'+
	        		'<div class="sy-td-danger"  data-container="body" data-toggle="popover" data-placement="bottom"  data-content=""><i class="fa fa-exclamation-triangle" aria-hidden="true"></i></div>'+
	        		'<ul class="dropdown-menu li-width f-input-li yaopinmcul"   style="display:none;width:260%" role="menu"></ul>'+
    				'</td>'+
    				'<td class="w-jiequ">';
    				if(this.baozhuanggg !=""){
    					content +='<span class="spanBaozhuanggg">'+this.baozhuanggg+'</span>';
    				}else{
    					content +='<span class="spanBaozhuanggg guige-color">包装规格</span>';
    				}
    				content +='</td>'+
    				'<td class="wm-dosage-father left">';
    				if(this.zhensuoypid!=null) {
    					content += '<input type="text" readonly="readonly" class="wm-dosage cgmdc-input51 inp-select" name="danjia" autocomplete="off" data-rule="单价:range[0~];length(~8)" data-target="#msg_holder" value="'+roundUtil(this.danjia,8)+'">元/';
    				}else{
    					content += '<input type="text" readonly="readonly" class="wm-dosage cgmdc-input51 inp-select" name="danjia" autocomplete="off" data-rule="单价:range[0~];length(~8)" data-target="#msg_holder" value="'+roundUtil(this.danjia, 8)+'">元/';
    				}
    				content +='<span class="spanKaiyaodw">'+this.dangedw+'</span>'+
		          	'</td>'+
		          	'<td class="wm-dosage-father wm-dosage-father2 dropdown-search-li">'+
		          	'<input type="text" class="wm-number cgmdc-input61" value="'+this.shuliang+'" name="kaiyaoliang" placeholder="数量" autocomplete="off" data-rule="数量:integer[+];length(~8)" data-target="#msg_holder" > ';
		          	if(this.chailingbz == null){
		          		content +='<input class="wm-dosage dropdown-toggle cgmdc-input62" value="'+this.danwei+'" placeholder="单位" data-rule="数量单位:length(1~10)" readonly="readonly" autocomplete="off" data-target="#msg_holder" name="kaiyaodw">';
		          	}else if(this.chailingbz == 0){
		          		content +='<input class="wm-dosage dropdown-toggle cgmdc-input62" value="'+this.danwei+'" placeholder="单位" data-rule="数量单位:length(1~10)" readonly="readonly" autocomplete="off" data-target="#msg_holder" name="kaiyaodw">';
		          	}else{
		          		content +='<input class="wm-dosage dropdown-toggle cgmdc-input62" value="'+this.danwei+'" placeholder="单位" data-rule="数量单位:length(1~10)" autocomplete="off" data-target="#msg_holder" name="kaiyaodw" style="background-color:#fff;">'+
		          		'<ul class="dropdown-menu li-width f-input-li kaiyaodw"  style="display:none;top: 3.5rem;" role="menu">'+
		          		'<li>'+this.baozhuangdw+'</li>'+
		          		'<li>'+this.chailingdw+'</li>';
		          	}
    				content +='</ul>'+
		          	'</td >'+
		          	'<td style="text-align: right;padding-right:30px;"><span class="jine">'+roundUtil(this.jine, 8)+'</span>元</td>'+
		          	'<td>'+
		       		'<div class="g-mz-delete pointer2 shanchu" data-container="body" data-toggle="popover" data-placement="bottom"  data-content="清除本行"></div>'+
		          	'</td>'+
		          	'<input type="hidden" name="zsShangpinid"  value="'+this.zhensuohcid+'">'+
	       		   	'<input type="hidden" name="ptShangpinid" value="'+this.pingtaihcid+'">'+
					'<input type="hidden" name="shengchancj" value="'+this.shengchancj+'">'+
					'<input type="hidden" name="baozhuanggg" value="'+this.baozhuanggg+'">'+
					'<input type="hidden" name="baozhuangdw" value="'+this.baozhuangdw+'">'+					
					'<input type="hidden" name="gongyingshang" value="'+this.gongyingshang+'">'+
	       		   	'<input type="hidden" name="iszs" value="'+this.iszs+'">'+
	       		   	'<input type="hidden" name="leixing" value="1">'+
	       		   	'<input type="hidden" name="yaopinlx">'+
		          	'</tr>';
    			});
    		}
            content += '<tr>'+
            '<td>'+
            '<input type="text" class="sy-medicine-name dropdown-toggle yaopinmc" autocomplete="off" data-rule="商品名称:length(1~100)" name="shangpinmc" data-target="#msg_holder">'+
            '<div class="sy-td-warning"  data-container="body" data-toggle="popover" data-placement="bottom"  data-content=""><i class="fa fa-exclamation-triangle" aria-hidden="true"></i></div>'+
            '<div class="sy-td-danger"  data-container="body" data-toggle="popover" data-placement="bottom"  data-content=""><i class="fa fa-exclamation-triangle" aria-hidden="true"></i></div>'+
            '<ul class="dropdown-menu li-width f-input-li yaopinmcul"   style="display:none;width:260%" role="menu"></ul>'+
            '</td>'+
            '<td class="w-jiequ">'+
            '<span class="spanBaozhuanggg guige-color">包装规格</span>'+
            '</td>'+
            '<td class="wm-dosage-father left">'+
            '<input type="text" class="wm-dosage cgmdc-input51 inp-select" name="danjia" autocomplete="off" data-rule="单价:range[0~];length(~8)" data-target="#msg_holder">元'+
              '<span class="spanKaiyaodw"></span>'+
              '</td>'+
              '<td class="wm-dosage-father wm-dosage-father2 dropdown-search-li">'+
              '<input type="text" class="wm-number cgmdc-input61" name="kaiyaoliang" autocomplete="off" placeholder="数量" data-rule="数量:integer[+];length(~8)" data-target="#msg_holder" > '+
              '<input class="wm-dosage dropdown-toggle cgmdc-input62"  data-rule="数量单位:length(1~10)" placeholder="单位" autocomplete="off" data-target="#msg_holder" name="kaiyaodw">'+
              '<ul class="dropdown-menu li-width f-input-li kaiyaodw"  style="display:none;top: 3.5rem;" role="menu">'+
              '<li>盒</li>'+
             '<li>瓶</li>'+
             '<li>支</li>'+
             '<li>袋</li>'+
             '<li>丸</li>'+
             '<li>片</li>'+
             '<li>粒</li>'+
             '<li>板</li>'+
             '<li>克</li>'+
      		'<li>个</li>'+
             '</ul>'+
              '</td >'+
              '<td style="text-align: right;padding-right:30px;"><span class="jine"></span>元</td>'+
              '<td>'+
               '<div class="g-mz-delete pointer2 shanchu" data-container="body" data-toggle="popover" data-placement="bottom"  data-content="清除本行"></div>'+
              '</td>'+
              '<input type="hidden" name="zsJiuzhenzd" id="hizsJiuzhenzd">'+
              '<input type="hidden" name="zsShangpinid">'+
                  '<input type="hidden" name="ptShangpinid">'+
  				'<input type="hidden" name="shengchancj">'+
				'<input type="hidden" name="baozhuanggg">'+
				'<input type="hidden" name="gongyingshang">'+
				'<input type="hidden" name="baozhuangdw">'+				
                  '<input type="hidden" name="iszs" >'+
                  '<input type="hidden" name="leixing" >'+
                  '<input type="hidden" name="yaopinlx">'+
              '</tr>';
    		$('#tbodyShouyao').html(content);
    		$('[name="kaiyaoliang"]').trigger('keyup');	
		}
	});
}

//点击进入患者详情信息
$('.g-l-1r-top').click(function() {
	//通过id得到患者信息
	$.ajax({
	    type:"get",
	    //获取json文件路径
	    url:"./huanzhe/getHuanzheById",
	    //传送数据
	    data:{id:huanzheid},
	    async:false,
	    //请求成功完成后要执行的方法
	    success: function(data){
	    	//显示详情信息
	    	if(data!="") {
	    		var huanzhexx = data.huanzhexx;
	    		$('#xingming').text(huanzhexx.xingming);
	    		if(huanzhexx.xingbie != null){
	    			if(huanzhexx.xingbie==1) {
		    			$('#xingbie').text('男');
		    		}else if(huanzhexx.xingbie==2){
		    			$('#xingbie').text('女');
		    		}
	    		}else {
	    			$('#xingbie').css("display","none");
	    		}
	    		$('#nianling').text(huanzhexx.nianling);
	    		if(huanzhexx.nianling != null){
	    			if(huanzhexx.nianlingdw=='岁') {
		    			$(".dropdown-toggle").text('岁');
		    		}else if(huanzhexx.nianlingdw=='月'){
		    			$(".dropdown-toggle").text('月');
					}
	    		}else {
	    			$("#hz-nianlingdw").hide();
	    		}
	    		$('#shoujihm').text(huanzhexx.shoujihm);
	    		$('#shenfenzhm').text(huanzhexx.shenfenzhm);
	    		$('#jinjilxr').html(huanzhexx.jinjilxr);
	    		$('#jinjilxdh').html(huanzhexx.jinjilxdh);
	    		$('#xiangxizz').html(huanzhexx.xiangxizz);
	    		$('#guominshi').html(huanzhexx.guominshi);
	    		$('#div-guominshi').html(huanzhexx.guominshi);
	    		$('#beizhu').html(huanzhexx.beizhu);
	    		if(huanzhexx.shengfen != ''&&huanzhexx.shengfen != null){
	    			$("#hz-dizhi-sf").html(huanzhexx.shengfen+'/');
	    		}else {
	    			$("#hz-dizhi-sf").html('');
	    		}
	    		if(huanzhexx.chengshi != ''&&huanzhexx.chengshi != null){
	    			$("#hz-dizhi-cs").html(huanzhexx.chengshi+'/');
	    		}else {
	    			$("#hz-dizhi-cs").html('');
	    		}
	    		if(huanzhexx.quxian != ''&&huanzhexx.quxian != null){
	    			$("#hz-dizhi-qx").html(huanzhexx.quxian);
	    		}else {
	    			$("#hz-dizhi-qx").html('');
	    		}
	    		$("#fenzuid").html(huanzhexx.fenzuid);
	    		$("#id").html(huanzhexx.id);
	    		//显示关联人信息
	    		if(data.guanlianhzs!="") {
	    			$(data.guanlianhzs).each(function(){
	    				if(huanzhexx.id!=this.id) {
	    					var shoujihm = this.shoujihm;
							if(shoujihm==null) {
								shoujihm = "";
							}
		    				var str = '<tr value="'+this.id+'" class="medicine-item-tr"><td  class="hzn-name"><label class=" mi-table-label2" ></label></td>'+
				               '<td width="100%" class="hzn-name-td2"><label class=" mi-table-label2" >'+this.xingming+'&nbsp;&nbsp;&nbsp;'+shoujihm+'</label><img class="pointer2" src="'+path+'/image/shanc.png" alt="">'+
				               '<ul id="dropdownli" class="dropdown-menu li-width f-input-li"  style="display:none;" role="menu"></ul>'+
				               '</td>'+
				               '<td class="hzn-name"><label class="mi-table-label2" ></label></td>'+
				               '<td class="hzn-name-td4">'+
				               '</td>'+
			                '</tr>'
		    				$("#tbguanlr").append(str);
		    				$(".pointer2").css("display","none");
		    				var ids = $('#guanlianrids').val();
							$('#guanlianrids').val(ids+","+this.id);
							$("#fenzuid").val(this.fenzuid);
	    				}
	    				
	    			});
	    		}
	    	}
	    	
	    }
	});
	$("#mymodal-xxxx").modal();
});

	var A = 0;
	$("form").keyup(function(){
		A++;
	});	
	window.onbeforeunload = function(){
		 if(A>0){
			 return false;
		 }
	  };
	$('#btnQuxiao').click(function(){
		if(shoufeizt==2||shoufeizt==3){
			A=0;
		}
		if(A>0){
				$("#shouyao-form-info").modal();
			}else{
				window.location.href=  path+'/jump/toShouyao';
			}
		});
	$("#shouyao-form-queding").click(function(){
		$("#shouyao-form-info").modal('hide');
		//A=0;
		window.location.href=  path+'/jump/toShouyao';
	});
//加.00
function getFloatStr(num){  
    num += '';  
    num = num.replace(/[^0-9|\.]/g, ''); //清除字符串中的非数字非.字符
    if(/^0+/) //清除字符串开头的0
        num = num.replace(/^0+/, '');
    if(!/\./.test(num)) //为整数字符串在末尾添加.00
        num += '.00';  
    if(/^\./.test(num)) //字符以.开头时,在开头添加0
        num = '0' + num;  
//    num += '00';        //在字符串末尾补零  
//    num = num.match(/\d+\.\d{2}/)[0];    
    return num;
}; 
//金额转00
$(document).on("focusout",".cgmdc-input51",function(){
	var jine = $(this).val();
	   jine = getFloatStr(jine);
	   $(this).val(jine);
});
$("input[name=jine]").focusout(function(){
	var jine = $(this).val();
	   jine = getFloatStr(jine);
	   $(this).val(jine);
});
$("#div-allergy").on('input',function(){
		var tt = $("#div-allergy").html();
		if(tt.length>200){
			var text =tt.substring(0,199);
			$(this).addClass("n-invalid");
			$(this).attr("contenteditable",false);
			$("#g-allergy").attr('readonly',true);
			$("#div-allergy").html(text);
			$(this).click(function(){
				$("#g-allergy").attr('readonly',false);
				$(this).attr("contenteditable",true);
			});
			notifyWarning("可输入最大长度为200个字符");
		}else if(tt.length<200){
			$(this).removeClass("n-invalid");
			$(this).attr("contenteditable",true);
			$("#g-allergy").attr('readonly',false);
			$("#g-allergy").val(tt);
		}
        
	});
//回车转Tab
//检查控件是否可见
function checkobjvisual(e) {


    var objvis = false;
    if (e) {
        if (e.offsetHeight > 0) {
            objvis = true;
        }
        if (e.offsetTop > 0) {
            objvis = true;
        }
        
         if (e.readOnly == true) {
             objvis = false;  
       }  
      
    }
    return objvis;
}



//查找下一个控件
function focunext(next_id, v) {

    var code;
    if (!e) {
        var e = window.event;
    }
    if (e.keyCode) {
        code = e.keyCode;
    }
    else if (e.which) {
        code = e.which;
    }
    if (code == 13) {


        if (next_id)
        {document.getElementById(next_id).focus(); }
        else {
            var inputList = document.getElementsByTagName("input");
            // 循坏这个集合，包括了所有的input。
            var nextindex;
            nextindex = -1;
            for (i = 0; i < inputList.length; i++) {
                if (inputList[i] == document.activeElement) {
                    var acobj = document.activeElement;
                    nextindex = i + 1;
                    
                    while ((!checkobjvisual(inputList[nextindex])) && (nextindex <= inputList.length)) {
                        nextindex = nextindex + 1;
                    }
                    if (inputList[nextindex])  inputList[nextindex].focus();
                    break;
                }
            }
            
        }
    }
};
document.onkeydown = function(e) {
	    var code;
	    if (!e) {
	        var e = window.event;
	    }
	    if (e.keyCode) {
	        code = e.keyCode;
	    }
	    else if (e.which) {
	        code = e.which;
	    }
	    if (code == 8) {
	        if (document.activeElement.readOnly == true || document.activeElement.disabled == true)
	        { return false; }
	    }
	    if (code == 13) {
	        if (document.activeElement.type == "textarea") {
	            return;
	        }
	        if (document.activeElement.type != "submit" && document.activeElement.type != "button") {
	            event.keyCode = 9;
	            if(!enterToTab){
			    	enterToTab = true;
			    }else if(!isTxm){
			    	focunext();
			    }
	            isTxm = false;
	            return false;
	        };
	    }
};

// 医保信息点击读卡
$('#duka').click(function () {
	var leixing = $(this).siblings('.yibaoinfo-head-type').find('span').attr('chikalx');
	var chikalx;
	if (leixing == '1') {
		chikalx = '1';
	} else {
		chikalx = '0';
	}
	var count = 0;
	getYiBaojbrc(chikalx, count);
});

function getYiBaojbrc(chikalx, count) {
	$.ajax({
	    type: "POST",
	    url: path + "/yibao/getYbRenyuancbjbxx",
	    async: false,
	    data: {"chikalx": chikalx},
	    success: function(data) {
	    	var result = JSON.parse(data);
    		if (result.chenggongbz == YBMSG_YILIAOJGBHISNULL) {
    			notifyWarning('医疗机构编号未填写');
    			return false;
    		}	    	
	    	if (result.chenggongbz == MSG_FALI) {
	    		getDangqzedzcs();
	    		$('#yb-chongxinqd').val(data);
	    		$('#yb-chongxinqd').trigger('click');
	    		count++;
	    		if (count > 1) {
	    			notifyWarning('服务器忙碌,请稍后再试');
	    			return false;
	    		}
	    		getYiBaojbrc(chikalx,count);
	    	} else {
	    		var yewuzqh = $('#yb-yewuzqh').val();
	    		if (null != yewuzqh && yewuzqh !='') {
	    			updateYeWuzqh(yewuzqh);
	    		}
	    		$('#yb_duka').val(data);
	    		$('#yb_duka').trigger('click');
	    		var huanzhexx = getHuanzhexxBygrbh(higerenbh);
	    		if (null != huanzhexx) {
	    			fillHuanzhexx(huanzhexx);
					// 患者折叠信息显示
					$('.zhedie').show();
					$('.huanz-message-down').hide();
	    		} else {
	    			$('#huanzhexx').trigger('click');
	    		}
	    	}
	    }
	});
}
// 填充患者信息
function fillHuanzhexx(huanzhexx) {
		huanzheid = huanzhexx.id;
		$('#inputname').val(huanzhexx.xingming);
		$('#inputname').attr('readOnly','true');
		if(huanzhexx.nianling != null) {
			$('#inputage').val(huanzhexx.nianling);
		}
		
		$('#inputphone').val(huanzhexx.shoujihm || '');
		$('#g-allergy').val(huanzhexx.guominshi);
		$('#div-allergy').html(huanzhexx.guominshi);
		
		if(huanzhexx.xingbie== 1) {
			$('#nan').attr('checked','checked');
			$('#work-label').attr('class','radio-label cgbj-input on');
			$('#stop-label').attr('class','radio-label');
		}else if(huanzhexx.xingbie==2){
			$('#nv').attr('checked','checked');
			$('#stop-label').attr('class','radio-label on');
			$('#work-label').attr('class','radio-label cgbj-input');
		}
		
		if(huanzhexx.nianlingdw=='岁') {
			$("#f-le-nianlingdw").text('岁');
		}else {
			$("#f-le-nianlingdw").text('月');
		}
		
		$('#isNew').val(0);
		$('#huanzheid').val(huanzhexx.id);
		$(".f-input-li").css("display","none");
		$('#hz_serch').val("");
		$(this).parent().html("");
		$(".xiangxiandls").css('display','block');
		$('.g-1lm-icon ').css("display","block");
		if (getLiShijzcs(huanzheid) != 0) {
			$('.g-l-1r-bottom').css('display','inline-block');
		} else {
			$('.g-l-1r-bottom').css('display','none');
		}
}

// 医保读卡类型控制
$('#yibao-ybinfo .dropdown-menu li').click(function(){
	var showTarget = $(this).parent().siblings('.dropdown-toggle').find('span');
	var myText = $(this).text();
	if (myText == "医保卡号") {
		showTarget.text("医保卡号").attr('chikalx', YBDUKALX_SHIMINKA);
		$(this).text("病历本号");
		$(this).parent().siblings('.yibaoinfo-type-input').attr('placeholder','请插入医保卡后点击读卡').attr('readOnly','readOnly');
		$(this).parent().parent().siblings('.yibaoinfo-head-text').attr('readOnly','readOnly').css('opacity','0');
		
	} else {
		showTarget.text("病历本号").attr('chikalx', YBDUKALX_ZHENGLIBEN);
		$(this).parent().siblings('.yibaoinfo-type-input').attr('placeholder','请输入病例本号').attr('readOnly',false);
		$(this).parent().parent().siblings('.yibaoinfo-head-text').attr('readOnly',false).css('opacity','1');
		$(this).text("医保卡号");
	}
	$(this).parent().toggle();
});

// 医保最近三天就诊明细查询
$(".f-bz").on("click",function(){
	if ($('#higerenbh').val() == '') {
		notifyWarning("请先进行读卡!");
		return false;
	}
	$(this).addClass("on").siblings().removeClass("on");
	$('#yb-jiuzhenxxmx').val($(this).text());
	var YibaoParams = getYiBaorc();
	$('#yb-YiBaoParams').val(YibaoParams);
	$('#yb-jiuzhenxxmx').trigger('click');
});